import { defineStore } from 'pinia'
import { getUserInfo } from '@/api/login/index.js'
import { signoutRedirect } from '@/utils/oidc/index.js'
export const useUserStore = defineStore('user', {
  persist: {
    enabled: true
  },
  state() {
    return {
      userInfo: {
        hasLogin: false
      }
    }
  },
  getters: {
    // userId() {
    //   return this.userInfo?.id
    // },
    userName() {
      return this.userInfo?.userName
    },
    realname() {
      return this.userInfo?.realname
    },
    avatar() {
      return this.userInfo?.avatar
    },
    role() {
      return this.userInfo?.role || []
    },
    authority() {
      return this.userInfo.authority
    },
    hasLogin() {
      return this.userInfo.hasLogin
    },
    roleName() {
      return this.userInfo.roleName
    },
    deptName() {
      return this.userInfo.deptName
    }
  },
  actions: {
    async getUserInfos(payload) {
      try {
        const res = await getUserInfo(payload)
        if (res.code === 0) {
          this.setUserInfo({ hasLogin: true, ...res.data })
          return Promise.resolve(res.data.type)
        } else {
          this.setUserInfo()
          return Promise.resolve(res.code)
        }
      } catch (error) {
        console.error(error)
        this.setUserInfo()
      }
    },
    setUserInfo(userInfo = { hasLogin: false }) {
      this.userInfo = { ...this.userInfo, ...userInfo }
    }
  }
})
